//
// Created by breeze on 23/3/31.
//
#include <iostream>
#include "platform.h"
using namespace std;


TPlatform::TPlatform(int _number, double _x, double _y, int _busy_time = -1, bool _full = false)
:number(_number),x(_x),y(_y),busy_time(_busy_time),is_full(_full)
{
    // 材料格子初始化
    if (number == 4)
    {
        free_numbers.emplace(1);
        free_numbers.emplace(2);
    }
    else if (number == 5)
    {
        free_numbers.emplace(1);
        free_numbers.emplace(3);
    }
    else if (number == 6)
    {
        free_numbers.emplace(2);
        free_numbers.emplace(3);
    }
    else if (number == 7)
    {
        free_numbers.emplace(4);
        free_numbers.emplace(5);
        free_numbers.emplace(6);
    }
}

TPlatform::TPlatform(int _number, double _x, double _y)
:number(_number),x(_x),y(_y)
{
    this->busy_time = -1;
    this->is_full = false;

    // 材料格子初始化
    if (number == 4)
    {
        free_numbers.emplace(1);
        free_numbers.emplace(2);
    }
    else if (number == 5)
    {
        free_numbers.emplace(1);
        free_numbers.emplace(3);
    }
    else if (number == 6)
    {
        free_numbers.emplace(2);
        free_numbers.emplace(3);
    }
    else if (number == 7)
    {
        free_numbers.emplace(4);
        free_numbers.emplace(5);
        free_numbers.emplace(6);
    }
}


void TPlatform::Read(string &buff)
{
    //将buff字符串拆分成子串存入strs容器中

    if (can_use == 0)
        return;
    stringstream ss(buff);
    string substr;
    vector<string> strs;
    while (ss >> substr)
    {
        if (substr.find_first_not_of(' ') != std::string::npos)
            strs.push_back(substr);
    }

    // 工作台编号
    // 坐标初始化
    this->x = stod(strs[1]);
    this->y = stod(strs[2]);

    // 剩余生产时间
    this->busy_time = stoi(strs[3]);
    if (number <= 3 || number >= 8)
        return;  ///> 主要是方便读取检查1-3是否可买

    if (busy_time > 0 && pre_sel_food.empty() && busy_time < 180)
    {
        // 查看是否可以预定，可以预定就放到presell里面；注：-1也能表示代表full,
        bool t = false;
        for (auto it: working_numbers)
        {
            // 检查是有人对该数字进行了预定
            if (it.second.second == number)
            {
                // 有人预定
                t = true;
                break;
            }
        }

        if (!t)
        {
            // 没人预定
            pre_sel_food.emplace(number);  ///> 放到可预定队列
        }
    }

    // 原材料格状态
    int decimal_num = stoi(strs[4]);  ///> 获得输入的十进制数
    bitset<8> binary(decimal_num);
    for (int i = 1; i <= 7; i++)
    {
        // 把空闲且不被占用的材料格放到free_numbers;
        if (binary[i] == 1)
        {
            // 把送达的物品放到已有材料格子中
            if(have_numbers.find(i) == have_numbers.end())
            {
                int t = 0;
                for (auto it: working_numbers)
                {
                    if(it.second.second == i)
                    {
                        // 检查数字在里面吗
                        t = 1;
                        break;
                    }
                }

                if (t == 1)
                    cerr << "whorking_number err\n";

                cerr<<i<<"   "<<working_numbers.size()<<" "<<have_numbers.size()<<" "<<free_numbers.size()<<" "<<'\n';
                have_numbers.emplace(i);
                free_numbers.erase(i);
                cerr<<i<<"   "<<working_numbers.size()<<" "<<have_numbers.size()<<" "<<free_numbers.size()<<" "<<'\n';
            }
        }
        else
        {
            // 把空闲格子放回去
            if (number == 4 && (i == 1 || i == 2))
            {
                int t = 0;
                for (auto it: working_numbers)
                {
                    if (it.second.second == i)
                    {
                        //检查数字在里面吗
                        t = 1;
                        break;
                    }
                }

                if (t == 0 && free_numbers.find(i) == free_numbers.end())
                {
                    // 没人正在送,且上次没添加
                    free_numbers.emplace(i);
                    have_numbers.erase(i);
                }
            }
            else if (number == 5 && (i == 1 || i == 3))
            {
                int t = 0;
                for (auto it: working_numbers)
                {
                    if (it.second.second == i)
                    {
                        // 检查数字在里面吗
                        t = 1;
                        break;
                    }
                }

                if (t == 0 && free_numbers.find(i) == free_numbers.end())
                {
                    // 没人正在送
                    free_numbers.emplace(i);
                    have_numbers.erase(i);
                }
            }
            else if (number == 6 && (i == 2 || i == 3))
            {
                int t = 0;
                for(auto it: working_numbers)
                {
                    if (it.second.second == i)
                    {
                        // 检查数字在里面吗
                        t = 1;
                        break;
                    }
                }
                if (t == 0 && free_numbers.find(i) == free_numbers.end())
                {
                    // 没人正在送
                    free_numbers.emplace(i);
                    have_numbers.erase(i);
                }
            }
            else if (number == 7 && (i == 4 || i == 5 || i == 6))
            {
                int t = 0;
                for (auto it: working_numbers)
                {
                    if(it.second.second == i)
                    {
                        //检查数字在里面吗
                        t = 1;
                        break;
                    }
                }

                if (t == 0 && free_numbers.find(i) == free_numbers.end())
                {
                    // 没人正在送
                    free_numbers.emplace(i);
                    have_numbers.erase(i);
                }
            }
        }
    }

    // 产品格状态
    if (stoi(strs[5]) == 1)
        this->is_full = true;  ///> 可以拿走

    if (is_full)
    {
        // 查看是否可以购买，可以购买就放到sell里面
        while (pre_sel_food.size()>1)
        {
            cerr<<"pla class full err\n";
            while(1);
        }

        if (pre_sel_food.size() == 1)
        {
            sell_food.emplace(*pre_sel_food.begin());
            pre_sel_food.clear(); //物品生产完毕时，未被预定
        }
        else
        {
            // 物品被预定了
            pre_sel_food.clear();
        }
    }
}